package cn.itrip.service.itripHotelOrder;
import cn.itrip.beans.pojo.ItripHotelTempStore;
import cn.itrip.beans.pojo.ItripOrderLinkUser;
import cn.itrip.beans.pojo.ItripUserLinkUser;
import cn.itrip.common.BigDecimalUtil;
import cn.itrip.mapper.itripHotelOrder.ItripHotelOrderMapper;
import cn.itrip.beans.pojo.ItripHotelOrder;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.Page;
import cn.itrip.mapper.itripHotelRoom.ItripHotelRoomMapper;
import cn.itrip.mapper.itripOrderLinkUser.ItripOrderLinkUserMapper;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

import cn.itrip.common.Constants;
@Service
public class ItripHotelOrderServiceImpl implements ItripHotelOrderService {

    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;
    @Resource
    private ItripHotelRoomMapper itripHotelRoomMapper;
    @Resource
    private ItripOrderLinkUserMapper itripOrderLinkUserMapper;

    public ItripHotelOrder getItripHotelOrderById(Long id)throws Exception{
        return itripHotelOrderMapper.getItripHotelOrderById(id);
    }

    public List<ItripHotelOrder>	getItripHotelOrderListByMap(Map<String,Object> param)throws Exception{
        return itripHotelOrderMapper.getItripHotelOrderListByMap(param);
    }

    public Integer getItripHotelOrderCountByMap(Map<String,Object> param)throws Exception{
        return itripHotelOrderMapper.getItripHotelOrderCountByMap(param);
    }

    public Map<String,Object> itriptxAddItripHotelOrder(ItripHotelOrder itripHotelOrder,
                                                        List<ItripUserLinkUser> linkUsers)throws Exception{
            //itripHotelOrder.setCreationDate(new Date());
            //return itripHotelOrderMapper.insertItripHotelOrder(itripHotelOrder);
        Map<String,Object> output = new HashMap<>();
        //新增
        if(itripHotelOrder.getId() == null){
            itripHotelOrder.setCreationDate(new Date());
            itripHotelOrderMapper.insertItripHotelOrder(itripHotelOrder);
        }else{ //修改
            //删除旧的linkUser
            itripOrderLinkUserMapper.deleteItripOrderLinkUserByOrderId(itripHotelOrder.getId());
            //修改
            itripHotelOrder.setModifyDate(new Date());
            itripHotelOrderMapper.updateItripHotelOrder(itripHotelOrder);
        }
        //orderLinkUser
        if(EmptyUtils.isNotEmpty(linkUsers)){
            ItripOrderLinkUser orderLinkUser = null;
            for(ItripUserLinkUser itripUserLinkUser:linkUsers){
                orderLinkUser = new ItripOrderLinkUser();
                BeanUtils.copyProperties(itripUserLinkUser,orderLinkUser);
                orderLinkUser.setOrderId(itripHotelOrder.getId());
                orderLinkUser.setLinkUserId(itripUserLinkUser.getId());
                orderLinkUser.setCreationDate(new Date());
                orderLinkUser.setCreatedBy(itripUserLinkUser.getId());
                itripOrderLinkUserMapper.insertItripOrderLinkUser(orderLinkUser);
            }
        }
        output.put("id",itripHotelOrder.getId());
        output.put("orderNo",itripHotelOrder.getOrderNo());
        return output;

    }

    public Integer itriptxModifyItripHotelOrder(ItripHotelOrder itripHotelOrder)throws Exception{
        itripHotelOrder.setModifyDate(new Date());
        return itripHotelOrderMapper.updateItripHotelOrder(itripHotelOrder);
    }

    public Integer itriptxDeleteItripHotelOrderById(Long id)throws Exception{
        return itripHotelOrderMapper.deleteItripHotelOrderById(id);
    }

    public Page<ItripHotelOrder> queryItripHotelOrderPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize)throws Exception{
        Integer total = itripHotelOrderMapper.getItripHotelOrderCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ItripHotelOrder> itripHotelOrderList = itripHotelOrderMapper.getItripHotelOrderListByMap(param);
        page.setRows(itripHotelOrderList);
        return page;
    }

    //一下是自己加的方法


    @Override
    public List<ItripHotelTempStore> queryRoomStore(Map<String, Object> params) throws Exception {
        //完善实时库存表，调用存储过程
        itripHotelOrderMapper.flushStore(params);
        //实时库存表结合订单表的临时记录（为支付）计算每一天的真实库存
        return itripHotelOrderMapper.queryRoomStore(params);
    }

    @Override
    public Boolean validateRoomStore(Map<String, Object> params) throws Exception{
        //用户输入count
        Integer count = (Integer)params.get("count");
        //查出最小库存
        List<ItripHotelTempStore> stores = itripHotelOrderMapper.queryRoomStore(params);
        if(stores.size()>0 && EmptyUtils.isNotEmpty(stores)){
            return stores.get(0).getStore()>count;
        }
        return false;
    }

    @Override
    public Map<String, Object> validateRoomStore2(Map<String, Object> params) throws Exception {
        //用户输入count
        Integer count = (Integer)params.get("count");
        //查出最小库存
        List<ItripHotelTempStore> stores = itripHotelOrderMapper.queryRoomStore(params);
        Map<String,Object> output = new HashMap<>();
        if(stores.size()>0 && EmptyUtils.isNotEmpty(stores)){
            output.put("flag",stores.get(0).getStore()>count);
            output.put("store",stores.get(0).getStore());
            return output;
        }else{
            output.put("flag",false);
            output.put("store",0);
            return output;
        }
    }

    @Override
    public BigDecimal getOrderPayAmount(int count, long roomId) throws Exception {
        double price = itripHotelRoomMapper.getItripHotelRoomById(roomId).getRoomPrice();
        return BigDecimalUtil.OperationASMD(count,price,BigDecimalUtil.BigDecimalOprations.multiply,
                2,BigDecimal.ROUND_DOWN);
    }
}
